package cn.edu.ujn.food.controller;

import cn.edu.ujn.food.entity.*;
import cn.edu.ujn.food.service.*;
import cn.edu.ujn.util.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@org.springframework.stereotype.Controller
public class TableController {
    @Autowired
    private UserService userService;
    @Autowired
    private RestaurantService restaurantService;
    @Autowired
    private TableService tableService;
    @ModelAttribute("restaurantMap")
    public Map<Integer, String> getRestaurantMap() {
        List<Restaurant> restaurants = restaurantService.findAll();
        Map<Integer, String> restaurantMap = new HashMap<>();
        for (Restaurant restaurant : restaurants) {
            restaurantMap.put(restaurant.getRestId(), restaurant.getRestName());
        }
        return restaurantMap;
    }

    @GetMapping("/table/{id}")
    @ResponseBody
    public ResponseEntity<Table> getTableById(@PathVariable("id") Integer tableId) {
        Table table = tableService.queryById(tableId);
        if (table == null) {
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
        return new ResponseEntity<>(table, HttpStatus.OK);
    }

    @PutMapping("/table/{id}")
    @ResponseBody
    public ResponseEntity<Map<String, Object>> updateTable(@PathVariable("id") Integer tableId, @RequestBody Table table) {
        try {
            table.setTableId(tableId);
            tableService.update(table);
            return ResponseEntity.ok(Map.of("success", true, "message", "餐桌信息已成功更新"));
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                    .body(Map.of("success", false, "message", "更新失败: " + e.getMessage()));
        }
    }

    @DeleteMapping("/table/{id}")
    public ResponseEntity<Map<String, Object>> deleteTable(@PathVariable("id") Integer tableId) {
        try {
            boolean isDeleted = tableService.deleteById(tableId);
            if (isDeleted) {
                return ResponseEntity.ok(Map.of("success", true, "message", "餐桌信息删除成功！"));
            } else {
                return new ResponseEntity<>(Map.of("success", false, "message", "餐桌信息删除失败，餐桌不存在或已删除。"), HttpStatus.NOT_FOUND);
            }
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                    .body(Map.of("success", false, "message", "餐桌信息删除失败: " + e.getMessage()));
        }
    }

    @PostMapping("/table")
    @ResponseBody
    public String createTable(Table table) {
        try {
            tableService.insert(table);
            return "OK";
        } catch (Exception e) {
            return "FAIL";
        }
    }

    @GetMapping("/table")
    public String listTable(@RequestParam(value = "page", defaultValue = "1") Integer page,
                            @RequestParam(value = "row", defaultValue = "10") Integer rows, Model model, Table table) {
        table.setStart((page - 1) * rows);
        table.setRows(rows);
        Page<Table> page8 = tableService.selectAll(table);
        page8.setPage(page);
        model.addAttribute("page", page8);
        return "table";
    }
}


